<?php

/**
 * 上传类
 *
 */

class Upload {
  private $images = array('jpg', 'jpeg', 'gif', 'png', 'bmp');
  private $keyarr = array('name','type','tmp_name','size');
  private $config = array();

  function __construct($config=array()) {
    $this->config['save_dir']   = "caches/upload/";
    $this->config['save_type']  = 4;
    $this->config['max_size']   = 10 * 1024 * 1024;
    $this->config['ext']        = array('jpg','gif','png','bmp');
    $this->config['pre']        = '';
    $this->config['dir_once']   = '';
    $this->setconfig($config);
  }

  function __destruct() {

  }

  function setconfig($config=array()) {
    if (!empty($config) && is_array($config)){
      foreach($config as $key=>$value) {
        $this->config[$key] = $value;
      }
    }
  }

  function getsavename($filename) {
    switch($this->config['save_type']) {
      case 1: return $this->config['pre'].$filename; break;
      case 2: return $this->config['pre'].time().'_'.random(6).'_'.$filename; break;
      case 3: return $this->config['pre'].time().'_'.random(6).'.'.$this->getext($filename); break;
      case 4: return $this->config['pre'].time().'_'.random(6).'.'.$this->getext($filename); break;
      case 5: return $this->config['pre'].time().'_'.random(6).'.'.$this->getext($filename); break;
      default: return $this->config['pre'].$filename;
    }
  }

  function getsavedir() {
    switch($this->config['save_type']) {
      case 1: return ''; break;
      case 2: return ''; break;
      case 3: return ''; break;
      case 4: return date("Ymd").'/'; break;
      case 5: return date("Y").'/'.date("m").'/'.date("d").'/'; break;
      default: return '';
    }
  }


  function makepath($path) {
    $dirs = explode('/', dirname($path));
    $tmp = '';
    foreach ($dirs as $dir) {
      $tmp .= $dir . '/';
      if (!file_exists($tmp) && !@mkdir($tmp, 0777))
        return $tmp;
    }
    return true;
  }

  function getext($filename) {
    return strtolower(end(explode(".", $filename)));
  }

  function isallow($filename) {
    if(in_array($this->getext($filename),$this->config['ext'])) {
      return true;
    }
    else {
      return false;
    }
  }

  function isimage($filename) {
    if($this->isallow($filename) && in_array($this->getext($filename),$this->images)) {
      return true;
    }
    else {
      return false;
    }
  }

  function random($n) {
    if ($n < 1 || $n>10) return "";
    $ary_num = array(0,1,2,3,4,5,6,7,8,9);
    $return = "";
    for ($i=0;$i<$n;$i++) {
      $randn = rand(0,9-$i);
      $return .= $ary_num[$randn];
      $ary_num[$randn] = $ary_num[9-$i];
    }
    return $return;
  }

  function run($file) {
    $return = array();
    $filename = strtolower($file['name']);
    $return['filename']   = $filename;
    $return['save_dir']   = $this->getsavedir();		
		$return['save_name']  = $this->getsavename($filename);
    $return['savename']   = ($this->config['dir_once'] ? $this->config['dir_once'] : $this->config['save_dir']) . $return['save_dir'] . $return['save_name'];
		$return['ext']        = $this->getext($filename);
		$return['size']       = $file['size'];
		$return['isimage']    = $this->isimage($filename);
    if(!$this->isallow($filename)) { return false; }
    if($file['size'] > $this->config['max_size']) { return false; }
    if($this->makepath($return['savename']) !== true) { return false; }
    if(!move_uploaded_file($file["tmp_name"], $return['savename'])) {
      copy($file["tmp_name"], $return['savename']);
    }
    $this->config['pre'] = '';
    $this->config['dir_once'] = '';
    return $return;
  }

  function mult() {
    $return = array();
    foreach($_FILES as $_key=>$file) {
      $toggle = 1;
      foreach($this->keyarr as $k) {
        if(!isset($_FILES[$_key][$k])) {
          $toggle = 0;
        }
      }
      if($toggle==1) {
        $ret = array();
        $filename = strtolower($file['name']);
        $ret['filename']   = $filename;
        $ret['save_dir']   = $this->getsavedir();		
        $ret['save_name']  = $this->getsavename($filename);
        $ret['savename']   = ($this->config['dir_once'] ? $this->config['dir_once'] : $this->config['save_dir']) . $ret['save_dir'] . $ret['save_name'];
        $ret['ext']        = $this->getext($filename);
        $ret['size']       = $file['size'];
        $ret['isimage']    = $this->isimage($filename);
        if( $this->isallow($filename) &&
            ($file['size'] <= $this->config['max_size']) &&
            ($this->makepath($ret['savename']) == true) ) {
          if(!move_uploaded_file($file["tmp_name"], $ret['savename'])) {
            copy($file["tmp_name"], $ret['savename']);
          }
          $return[] = $ret;
        }
      }
    }
    $this->config['pre'] = '';
    $this->config['dir_once'] = '';
    return $return;
  }

}

?>